package com.wyh.controller.gfive;


import com.wyh.common.result.Result;
import com.wyh.common.utils.ali.AliOssUtil;
import com.wyh.domain.dto.gfive.InvoicedetailsDTO;
import com.wyh.domain.dto.gfive.ReceiptConfigurationDTO;
import com.wyh.domain.po.gfive.Invoicedetails;
import com.wyh.service.gfive.ReceiptConfigurationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;
import java.util.List;
import java.util.UUID;


@Slf4j
@Api(tags = "收据记录待处理")
@RestController
@RequestMapping("/notifications")
public class ReceiptConfigurationController {

    @Autowired
    private ReceiptConfigurationService receiptConfigurationService;

    /*
     * 分页查找所有收据信息
     * */
    @GetMapping("/search")
    @ApiOperation("分页查找所有收据信息")
    public Result<List<ReceiptConfigurationDTO>> findAll(
            @RequestParam(required = false) String noticenumber,
            @RequestParam(required = false) String customername,
            @RequestParam(required = false) BigDecimal minReceivable,
            @RequestParam(required = false) BigDecimal maxReceivable,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size) {

        List<ReceiptConfigurationDTO> all = receiptConfigurationService.findAll(noticenumber, customername, minReceivable, maxReceivable, page, size);
        Long count = receiptConfigurationService.countAll(noticenumber, customername, minReceivable, maxReceivable);
        return Result.success(all,count);
    }


    /*
     *  上传收据
     * */
    @PostMapping("/upReceipts")
    @ApiOperation("上传收据")
    public Result upReceipts(@RequestBody @Validated InvoicedetailsDTO invoicedetails) {

        receiptConfigurationService.upReceipts(invoicedetails);

        return Result.success();
    }

    /*
     * 收据预览
     * */
    @GetMapping("/viewReceipt")
    @ApiOperation("收据预览")
    public Result<List<Invoicedetails>> findInvoicedetails(
            @RequestParam()  String noticenumber) {

        List<Invoicedetails> all = receiptConfigurationService.findInvoicedetails(noticenumber);
        return Result.success(all);
    }


    /*
     * 上传图片
     * */
    @PostMapping("/uploadImg")
    @ApiOperation("上传图片")
    public Result upload(MultipartFile file) throws Exception {
        String of = file.getOriginalFilename();
        String filename = UUID.randomUUID() + of.substring(of.lastIndexOf("."));

        String url = AliOssUtil.uploadFile(filename, file.getInputStream());

        return Result.success(url);
    }


}
